using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; // File, Directory, etc.
using Microsoft.Office.Core;
//In order for this library to work, you must do the following - TODO:
//Project + Add Reference, COM, select Microsoft Exell 12 object library, and Excel 5 library

using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using System.Windows.Forms; // MessageBox

namespace GAscheduler
{
    static class CourseManager
    {
        static Course[] m_aCourses = new Course[400];
        static int m_iALength = 0;
        public static int ALength {get {return m_iALength;}} //get length of course array

        public static Course get(int i)
        {
            return m_aCourses[i];
        }
        
        public static void LoadExcel(string sFileName)
        {
            Microsoft.Office.Interop.Excel.Application m_xlApp;
            Microsoft.Office.Interop.Excel.Workbook m_xlWkb;
            Microsoft.Office.Interop.Excel.Worksheet m_xlWkSh;
            object misValue = System.Reflection.Missing.Value;
            
            //initialize excel document to use
            m_xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            m_xlWkb = m_xlApp.Workbooks.Open(sFileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            m_xlWkSh = (Microsoft.Office.Interop.Excel.Worksheet)m_xlWkb.Worksheets.get_Item(1);

            //initialize parts of a course
            string sSubject = null;
            string sCourseNum = null;
            string sTeacher = null;
            string sCapacity = null;
            //string sType = null;

          //TODO: Talk to registrar about putting Schedule Type and Capacity for the courses in the excel sheet
          //TODO: make cell spots constants declared earlier
            //load courses into array
            //access to cell from http://www.codeproject.com/KB/cs/Excel_Application_in_C_.aspx
            for (int i = 2; ((Excel.Range)m_xlWkSh.Cells[i, 4]).Value != null; i++)
            {
                sSubject = (string)((Excel.Range)m_xlWkSh.Cells[i, 4]).Value;
                sCourseNum = (string)((Excel.Range)m_xlWkSh.Cells[i, 6]).Value;
                sTeacher = (string)((Excel.Range)m_xlWkSh.Cells[i, 26]).Value;
                //This will have a variable value later when it is actually on the excel sheet
                sCapacity = "20";  //(string)((Excel.Range)m_xlWkSh.Cells[i,?]).Value;
                //sType = (string)((Excel.Range)m_xlWkSh.Cells[i,?]).Value;
                m_aCourses[i - 2] = new Course(sSubject, sCourseNum, sTeacher, sCapacity /*, sType*/);
                m_iALength ++; //accumulate total length of array
            }

            m_xlWkb.Close(true, misValue, misValue);
            m_xlApp.Quit();
            releaseObject(m_xlWkSh);
            releaseObject(m_xlWkb);
            releaseObject(m_xlApp);
        }

        //this function was from http://csharp.net-informations.com/excel/csharp-open-excel.htm
        private static void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Unable to release the Object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }
    }
}
